/**
 * 
 */
package com.elite.groovy.callback

import groovy.json.* //导入grooby的json解析api包
import java.util.Map;
import com.elite.groovy.util.EliteBean;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.wisdge.utils.RandomUtils;
import com.elite.groovy.util.Util;
import cn.com.elite.PropertiesUtil;

import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
/**
 * 
 * @author luper
 *
 */

 public  class JiNuoStatusService implements ServiceBackInvoke{
 	public static final Log log = LogFactory.getLog(JiNuoStatusService.class);
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response){
	 log.debug("-----Begin Systeam----------");
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
	
		Util u = new Util();
		String requestJSONStr =u.getRequestBody(request, "UTF-8");
		log.debug("requestJSONStr:" + requestJSONStr);
		request.getParameter("interfacetype")
		//获取接口请求类型：status 吉诺状态回传/images 吉诺图片回传(吉诺定义的格式)
		String v_interfacetype=request.getParameter("interfacetype");
		log.debug("v_interfacetype:"+v_interfacetype);
		
		//报文解析(通用字段)
		JSONObject json  = new JSONObject().fromObject(requestJSONStr);
		String v_orderId = json.getString("orderId");    		 //订单ID
		log.debug("v_orderId:"+v_orderId);
	

		//数据校验
		def querysql1="select count(*) count1 from objective where workorderid='"+v_orderId+"'";
		def count1="";
		def list1=eliteBean.chaxun_sql(querysql1,"dataSource");
		log.debug("list-------------------------------"+list1);
		for(def k=0;k<list1.size;k++){
		def map1=list1[k];
		def v_count1=map1.get("count1");
		log.debug("v_count1---v_count1-----v_count1----"+v_count1);
		
		if(v_count1>0){
		//吉诺救援状态回传业务逻辑
		if(v_interfacetype=='status'){	
			String v_orderNumber = json.getString("orderNumber");    //订单编号
			String v_status = json.getString("status");				 //本次回调的状态
			String v_sign = json.getString("sign");                  //本次回调的签名值
			String v_time = json.getString("time");                  //时间戳
			//String v_driverName = json.getString("driverName"); 	 //技师名字
			//String v_driverPhone = json.getString("driverPhone");    //技师手机
			//String v_driverLng = json.getString("driverLng");		 //技师所在位置经度
			//String v_driverLat = json.getString("driverLat");		 //技师所在位置纬度
			//String v_driverPlate = json.getString("driverPlate");	 //司机的车牌号码
			log.debug("v_status:"+v_status);
			log.debug("v_sign:"+v_sign);
			log.debug("v_time:"+v_time);

		if	(v_status != null) {	
			if (v_status=="AUDIT"){
				 v_status = "订单已审核";}
			else if (v_status == "DISTARIBUTION"){
				 v_status =  "派车";}
			else if (v_status == "CANCELRESCUE"){
				 v_status =  "取消订单";}
			else if (v_status == "ACCEPTED"){
				 v_status =  "技师已接单";}
			else if (v_status == "SETOFFED"){
				 v_status =  "技师已出发";}
			else if (v_status == "ARRIVE_RESCUE"){
				 v_status =  "到达救援地";}
			else if (v_status == "DESTINATION"){
				 v_status = "到达目的地";}
			else if (v_status == "TRUCK_LOADING"){
				 v_status = "技师开始装车";}
			else if (v_status == "UNLOADING"){
				 v_status = "技师开始卸车";}
			else if (v_status == "COMPLETED"){
				 v_status = "技师已完工";}
			else if (v_status == "CANCELFREE"){
				 v_status = "取消无费";}
			else if (v_status == "CANCEL"){
				 v_status = "取消有费";}
			else if (v_status == "FAIL"){
				 v_status = "救援失败";
			}
		//服务商操作轨迹记录	 
		def updatesql1="insert into workorderstep3(WORKORDER_GUID, WORKORDERSTEP_GUID,OLDHANDLEBY_ID, OLDHANDLEGROUP_ID,WORKORDERSTEPSTATUS,COMMENTS,MODIFIEDDATE )values((select objective_guid from objective where workorderid='"+v_orderId+"'),sys_guid(),'000143','16J64F','FWSHD','"+v_status+"',sysdate)";
		def updatesqlresult1=eliteBean.update(updatesql1,"dataSource");		
		log.debug("updatesqlresult1:"+updatesqlresult1);
		
			def it= json.get("extraParam");
			it=it.get("thReport");
			
			//更新救援费用、救援结果
		if(v_status=="取消订单"||v_status=="取消无费"||v_status=="取消有费" ||v_status=="救援失败" ||v_status=="技师已完工"){
			def aRescueAmount=it.getString("aRescueAmount");
			log.debug("--------aRescueAmount----------------:"+aRescueAmount);
			eliteBean.update("update tabcdh_vas_list set CASE_COST='"+aRescueAmount+"',help_result='"+v_status+"',DD_CASEID='"+v_orderNumber+"' where order_id='"+v_orderId+"'","dataSource");			
		}	
		//更新订单状态
		if(v_status=="技师已完工"){
			eliteBean.update("update tabcdh_vas_list set orderstatus='1' where order_id='"+v_orderId+"'","dataSource");
		}
		if(v_status=="救援失败"){
			eliteBean.update("update tabcdh_vas_list set orderstatus='2' where order_id='"+v_orderId+"'","dataSource");
		}
			
		if(v_status=="取消订单"||v_status=="取消无费"||v_status=="取消有费"){	
		def orderRemark=it.getString("orderRemark");
		//更新tabc取消信息字段
			def updatesql2="update tabcdh_vas_list set CANCELER='吉诺服务商',CANCEL_TIME='"+v_time+"',CANCEL_FLAG=1,DD_CASEID='"+v_orderNumber+"',cancel_reason='"+orderRemark+"',orderstatus='3' where order_id='"+v_orderId+"'";
			def updatesqlresult2=eliteBean.update(updatesql2,"dataSource");
			log.debug("updatesqlresult2:"+updatesqlresult2);
		//更新obj
			def updatesql3="update objective set OBJECTIVESTATUS='CLOSE' where workorderid='"+v_orderId+"'";
			def updatesqlresult3=eliteBean.update(updatesql3,"dataSource");
			log.debug("updatesqlresult3:"+updatesqlresult3);
		//删除elitetask
			def updatesql4="delete from elitetask where objective_guid in (select objective_guid from objective where workorderid='"+v_orderId+"')";
			def updatesqlresult4=eliteBean.update(updatesql4,"dataSource");
			log.debug("updatesqlresult4:"+updatesqlresult4);				
		}
             
			//返回成功结果
			JSONObject  jsonALL3 = new JSONObject();
			jsonALL3.put("code", "200");
			jsonALL3.put("msg", "success");
			return jsonALL3;
		}
        }
		
		
		

		
		//吉诺照片信息回传业务逻辑
		if(v_interfacetype=='images'){
			String v_orderNumber = json.getString("orderNumber");    //订单编号
			String v_status = json.getString("status");				 //本次回调的状态
			String v_sign = json.getString("sign");                  //本次回调的签名值
			String v_time = json.getString("time");                  //时间戳		
			String v_filePath = json.getString("filePath");		 	 //照片路径(为https的网页路径),第三方系统可以自行下载该照片或者直接使用该路径
			log.debug("v_filePath:"+v_filePath);
		    
			if (v_status=='AUDI'){
				 v_status = '订单已审核';}
			else if (v_status == 'DISTARIBUTION'){
				 v_status =  '派车';}
			else if (v_status == 'CANCELRESCUE'){
				 v_status =  '取消订单';}
			else if (v_status == 'ACCEPTED'){
				 v_status =  '技师已接单';}
			else if (v_status == 'SETOFFED'){
				 v_status =  '技师已出发';}
			else if (v_status == 'ARRIVE_RESCUE'){
				 v_status =  '到达救援地';}
			else if (v_status == 'DESTINATION'){
				 v_status = '到达目的地';}
			else if (v_status == 'TRUCK_LOADING'){
				 v_status = '技师开始装车';}
			else if (v_status == 'UNLOADING'){
				 v_status = '技师开始卸车';}
			else if (v_status == 'COMPLETED'){
				 v_status = '技师已完工';}
			else if (v_status == 'CANCELFREE'){
				 v_status = '取消无费';}
			else if (v_status == 'CANCEL'){
				 v_status = '取消有费';}
			else if (v_status == 'FAIL'){
				 v_status = '救援失败';
			}
			
			//更新数据	
			def updatesql1="insert into uploadlist (UPLOADGUID, CREATEDATE, PATH ,FILENAME ,STAFF_ID, GUID,CONFIDENTIAL_TYPE ,UPLOAD_STATUS) values(sysguid,sysdate,'"+v_filePath+"','"+v_status+"','000143',(select objective_guid from objective where workorderid='"+v_orderId+"'),'1','0')";
			def updatesqlresult1=eliteBean.update(updatesql1,"dataSource");
			log.debug("updatesqlresult1:"+updatesqlresult1);
				
			//返回成功结果
			JSONObject  jsonALL3 = new JSONObject();
			jsonALL3.put("code", "200");
			jsonALL3.put("msg", "success");
			return jsonALL3;
			}
			
		//吉诺GPS信息回传业务逻辑
		if(v_interfacetype=='GPS'){	 
			
			String pointList = json.getString("pointList");
			log.debug("pointList:"+pointList);
			JSONArray LIST1 = json.getJSONArray("pointList");
			log.debug("吉诺GIS路径信息列表:"+LIST1);	
			LIST1.each{ 
				def v_longitude=it.longitude;                     //经度
				log.debug("v_longitude:"+v_longitude); 
				def v_latitude=it.latitude;                       //纬度
				log.debug("v_latitude:"+v_latitude); 				
				def v_loc_time=it.loc_time;                       //时间
				log.debug("v_loc_time:"+v_loc_time); 			
			
			//更新数据	
			def updatesql1="update tabcdh_vas_list set jygps_lat='"+v_latitude+"',jygps_lon='"+v_longitude+"',jsgps_modifydate='"+v_loc_time+"' where ORDER_ID='"+v_orderId+"'";
			def updatesqlresult1=eliteBean.update(updatesql1,"dataSource");
			log.debug("updatesqlresult1:"+updatesqlresult1);
			}	
			//返回成功结果
			JSONObject  jsonALL3 = new JSONObject();
			jsonALL3.put("code", "200");
			jsonALL3.put("msg", "success");
			return jsonALL3;
		}

		else{
			JSONObject  jsonALL3 = new JSONObject();
			jsonALL3.put("code", "400");
			jsonALL3.put("msg", "failure");
			return jsonALL3;
			
			}
		}
		}
	}			
}

